﻿using System;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using Htm2Pdf.Convertor;
using Htm2Pdf.Data;
using NLog;

namespace Htm2Pdf.ConvertorTester
{
    class Program
    {

        static Logger _logger = LogManager.GetLogger("errorlogger");


        static void Main(string[] args)
        {
            string s = AppDomain.CurrentDomain.BaseDirectory;
            string wkHtml = Path.Combine(s, "wkhtmltopdf.exe");

            string[] sites = new string[]
                                 {
                                     "http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express",
                                    "utro.ru",
                                    "demotivation.ru",
                                    "vz.ru",
                                     "www.inosmi.ru", "http://lenta.ru"};
                
            try
            {

                string connStr = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;

                using (Database db = new Database(connStr))
                {
                    db.Connect();

                    FoldersManager fld = new FoldersManager(db);

                    foreach (var site in sites)
                    {

                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();

                        string newFolder = fld.CreateFolder();

                        int elapsed = 0;
                        string status = "OK";

                        try
                        {
                            PdfConvertor.Convert(site, wkHtml, newFolder);
                            stopwatch.Stop();

                            elapsed = (int)stopwatch.Elapsed.TotalSeconds;
                        }
                        catch (PdfConvException ex)
                        {
                            _logger.Log(LogLevel.Warn, ex.Message);     

                            status = ex.Message;
                        }


                        db.AddResult(site, elapsed, status);
                        stopwatch.Reset();
                    
                    }
                }


            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, ex.Message);  
                Console.WriteLine(ex.ToString());
            }

            Console.ReadKey(true);
        }
    }
}
